<template>
<div>
  <div class="xinxi">
    <el-form ref="form" :model="form" label-width="80px" :inline="true">
      <el-form-item label="城市等级">
        <el-select v-model="form.regionLevel" placeholder="请选择" style="width: 150%" clearable>
          <el-option label="一线城市" value="1"></el-option>
          <el-option label="二线城市" value="2"></el-option>
          <el-option label="三线城市" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-button type="primary" @click="findPage()" style="margin-left: 50%">查询</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
      <br>
      <el-form-item label="" style="margin-left: 80px">
        <el-input v-model="form.regionName" style="width:  150%" placeholder="请输入城市名称" clearable></el-input>
      </el-form-item>
    </el-form>
  </div>
  <el-table
      :data="tableData"
      style="width: 100%">
    <el-table-column
        prop="regionId"
        label="序号">
    </el-table-column>
    <el-table-column
        prop="regionLevel"
        label="城市等级">
      <template slot-scope="scope">
        {{ scope.row.regionLevel == 1 ? "一线城市" : scope.row.regionLevel == 2 ? "二线城市" :"三线城市" }}
      </template>
    </el-table-column>
    <el-table-column
        prop="regionName"
        label="城市">
    </el-table-column>
    <el-table-column
        prop="updates"
        label="更新人">
    </el-table-column>
    <el-table-column
        prop="finallyTime"
        label="最后一次更新时间">
    </el-table-column>
    <el-table-column
        label="操作">
      <template slot-scope="scope">
        <el-row>
          <el-button type="text" size="small" @click="toUpdate(scope.row.regionId)" round>修改</el-button>
        </el-row>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[3, 10, 20, 30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
  </el-pagination>
  <el-dialog
      title="城市修改"
      :visible.sync="dialogVisible"
      width="30%"
     >
    <el-form :model="ruleForm"  ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="城市名称" prop="" >
        <el-input v-model="ruleForm.regionName" placeholder="" style="width: 62%" disabled></el-input>
      </el-form-item>
      <el-form-item label="城市等级">
        <el-select v-model="ruleForm.regionLevel" placeholder="请选择"  >
          <el-option label="一线城市" :value="1"></el-option>
          <el-option label="二线城市" :value="2"></el-option>
          <el-option label="三线城市" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateById()" center>修改</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</div>
</template>
<script>
import axios from "axios";
export default {
  name: "CityTier",
  data(){
    return{
      form:{
        regionLevel: '',
        regionName: '',
      },
      tableData: [],
      pageNum: 1,
      pageSize: 3,
      total: 0,
      ruleForm: {},
      dialogVisible: false,
    }
  },
  methods:{
    handleCurrentChange(val) {
      this.pageNum = val
      this.findPage();
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.findPage();
    },
    findPage() {
      axios.post(`http://localhost:9999/api-function/sysRegion/findPage?pageNum=${this.pageNum}&pageSize=${this.pageSize}`, this.form).then(res => {
        this.tableData = res.data.data.list
        this.total = res.data.data.total
      })
    },
    toUpdate(regionId) {
      axios.post(`http://localhost:9999/api-function/sysRegion/toUpdate?regionId=${regionId}`).then(res => {
        this.ruleForm = res.data.data;
        console.log(res.data.data.regionName)
        this.dialogVisible = true;
      })
    },
    updateById() {
          axios.post(`http://localhost:9999/api-function/sysRegion/updateById`,this.ruleForm).then(res => {
            if (res.data.code == 200) {
              this.$message.success("修改成功")
            } else {
              this.$message.error("添加失败")
            }
            this.dialogVisible = false;
            this.findPage();
          })
    },
  },
  created(){
    this.findPage();
  },
}
</script>
<style scoped>

</style>
